package com.example.dirviesystem.student.controller;

import com.example.dirviesystem.config.R;
import com.example.dirviesystem.entity.CoachInfo;
import com.example.dirviesystem.entity.StudentInfo;
import com.example.dirviesystem.repository.CoachInfoRe;
import com.example.dirviesystem.repository.StudentInfoRe;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@Slf4j
public class LoginControl {
    @Autowired
    private StudentInfoRe studentInfoRe;

    @Autowired
    private CoachInfoRe coachInfoRe;
    /**
     * 登录接口
     * TODO:教练+管理员登录
     */
    @GetMapping("/login")
    public R<Object> login(@RequestParam ("role") String role,
                           @RequestParam("username") String username,
                           @RequestParam("password") String password) throws  Exception {
        log.info("登录接口，参数：role={}, username={}, password={}", role, username, password);
        if (Objects.equals(role, "student")){
            StudentInfo studentInfo = studentInfoRe.findByAndIdNumberAndPassword(username, password);
            if (studentInfo != null) {
                return R.success(studentInfo);
            }else {
                return R.fail("用户名或密码错误");
            }
        } else if (Objects.equals(role, "admin")){
            return R.success(null);
        }  else if (Objects.equals(role, "coach")) {
            CoachInfo coachInfo = coachInfoRe.findByIdNumberAndPassword(username, password);
            if (coachInfo != null) {
                return R.success(coachInfo);
            } else {
                return R.fail("用户名或密码错误");
            }
        }  else {
            return R.fail("角色错误");
        }
    }

}

